﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mvvm.Models;

namespace Mvvm.Services;

public class ChatTimeReportService : IChatTimeReportService
{
    //聊天数据
    private List<ChatData> _chatDataList {get; set;}
    
    //储存对话相关时间
    private List<ChatConversation> _conversationList {get; set;}

    public List<ChatConversation> getConversationList()
    {
        return _conversationList;
    }
    
    private double _conversationAverageDuration {get; set;} = 0.0;
    
    private double _conversationAverageInterval {get; set;} = 0.0;
    
    private double _userAverageResponseTime {get; set;} = 0.0;
    
    private int _mylessThanOneMinute {get; set;} = 0;
    
    private int _mylessThanFiveMinute {get; set;} = 0;
    
    private int _mylessThanTenMinute {get; set;} = 0;
    
    private int _mymoreThanTenMinute {get; set;} = 0;
    
    private int _yourlessThanOneMinute {get; set;} = 0;
    
    private int _yourlessThanFiveMinute {get; set;} = 0;
    
    private int _yourlessThanTenMinute {get; set;} = 0;
    
    private int _yourmoreThanTenMinute {get; set;} = 0;

    public void createConverstionList(List<ChatData> chatDataList)
    {
        _chatDataList = chatDataList;
        _conversationList = new List<ChatConversation>();
        for (int i = 0; i < _chatDataList.Count; i++)
        {
            //所有的第一句话
            if (i == 0)
            {
                ChatConversation newChatConversation = new ChatConversation(_chatDataList[i].Timestamp);
                newChatConversation.addTime(_chatDataList[i].Timestamp, _chatDataList[i].Username);
                _conversationList.Add(newChatConversation);
                continue;
            }

            //一个对话的第一句话
            if ((_chatDataList[i].Timestamp - _chatDataList[i - 1].Timestamp).TotalHours > 12)
            {
                _conversationList.Last().endConversation();
                ChatConversation newChatConversation = new ChatConversation(_chatDataList[i].Timestamp);
                newChatConversation.addTime(_chatDataList[i].Timestamp, _chatDataList[i].Username);
                _conversationList.Add(newChatConversation);
                if (i == _chatDataList.Count - 1)
                {
                    _conversationList.Last().endConversation();
                }
                continue;
            }

            //所有的最后一句话
            if (i == _chatDataList.Count - 1)
            {
                _conversationList.Last().addTime(_chatDataList[i].Timestamp, _chatDataList[i].Username);
                _conversationList.Last().endConversation();
                continue;
            }
            
            _conversationList.Last().addTime(_chatDataList[i].Timestamp, _chatDataList[i].Username);
        }
    }

    public double computeAverageDuration()
    {
        for (int i = 0; i < _conversationList.Count; i++)
        {
            _conversationAverageDuration += _conversationList[i].getDuration().TotalMinutes;
        }
        _conversationAverageDuration /= _conversationList.Count;
        return _conversationAverageDuration;
    }

    public double computeAverageInterval()
    {
        for (int i = 1; i < _conversationList.Count; i++)
        {
            _conversationAverageInterval += (_conversationList[i].getStartTime() - _conversationList[i - 1].getEndTime()).TotalHours;
        }
        _conversationAverageInterval /= (_conversationList.Count - 1);
        // Console.WriteLine("Average Interval: " + _conversationAverageInterval + " hours");
        return _conversationAverageInterval;
    }

    public double computeUserResponseTime(string username, int flag)
    {
        if (flag == 0)
        {
            for (int i = 0; i < _conversationList.Count; i++)
            {
                for (int j = 0; j < _conversationList[i].getTimeArray().Count; j++)
                {
                    if (_conversationList[i].getUserArray()[j] == username && j != 0 && _conversationList[i].getUserArray()[j - 1] != username)
                    {
                        double responseTime =
                            (_conversationList[i].getTimeArray()[j] - _conversationList[i].getTimeArray()[j - 1])
                            .TotalMinutes;
                        _userAverageResponseTime += responseTime;
                        if (responseTime < 1)
                        {
                            _mylessThanOneMinute++;
                        }else if (responseTime < 5)
                        {
                            _mylessThanFiveMinute++;
                        }else if (responseTime < 10)
                        {
                            _mylessThanTenMinute++;
                        }
                        else
                        {
                            _mymoreThanTenMinute++;
                        }
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < _conversationList.Count; i++)
            {
                for (int j = 0; j < _conversationList[i].getTimeArray().Count; j++)
                {
                    if (_conversationList[i].getUserArray()[j] == username && j != 0 && _conversationList[i].getUserArray()[j - 1] != username)
                    {
                        double responseTime =
                            (_conversationList[i].getTimeArray()[j] - _conversationList[i].getTimeArray()[j - 1])
                            .TotalMinutes;
                        _userAverageResponseTime += responseTime;
                        if (responseTime < 1)
                        {
                            _yourlessThanOneMinute++;
                        }else if (responseTime < 5)
                        {
                            _yourlessThanFiveMinute++;
                        }else if (responseTime < 10)
                        {
                            _yourlessThanTenMinute++;
                        }
                        else
                        {
                            _yourmoreThanTenMinute++;
                        }
                    }
                }
            }
        }
        _userAverageResponseTime /= _conversationList.Count;
        return _userAverageResponseTime;
    }

    public List<int> getMyResponseTimeTimes()
    {
        List<int> responseTimes = new List<int>();
        responseTimes.Add(_mylessThanOneMinute);
        responseTimes.Add(_mylessThanFiveMinute);
        responseTimes.Add(_mylessThanTenMinute);
        responseTimes.Add(_mymoreThanTenMinute);
        return responseTimes;
    }
    
    public List<int> getYourResponseTimeTimes()
    {
        List<int> responseTimes = new List<int>();
        responseTimes.Add(_yourlessThanOneMinute);
        responseTimes.Add(_yourlessThanFiveMinute);
        responseTimes.Add(_yourlessThanTenMinute);
        responseTimes.Add(_yourmoreThanTenMinute);
        return responseTimes;
    }

    public DateTime computeFirstChatTime()
    {
        DateTime firstChatTime = _conversationList[0].getStartTime();
        // Console.WriteLine("第一次聊天的日期: " + firstChatTime);
        return firstChatTime;
    }

    public DateTime computeLastChatTime()
    {
        DateTime lastChatTime = _conversationList[_conversationList.Count - 1].getEndTime();
        // Console.WriteLine("最后一次聊天的日期: " + lastChatTime);
        return lastChatTime;
    }

    public double computeTotalChatTime()
    {
        double totalChatTime = _conversationAverageDuration * _conversationList.Count;
        // Console.WriteLine("总聊天时长：" + totalChatTime + " minutes");
        return totalChatTime;
    }

    public int computeConversationCount()
    {
        // Console.WriteLine("总聊天次数：" + _conversationList.Count);
        return _conversationList.Count;
    }
    
    public string ChangeTimeFormat(double minutes)
    {
        string returnString = "";

        if (minutes >= 60 * 24)
        {
            returnString += (int)(minutes / (60 * 24)) + "天";
            minutes -= (int)(minutes / (60 * 24)) * 60 * 24;
        }
        
        if (minutes >= 60)
        {
            returnString += (int)(minutes/60) + "小时";
            minutes -= (int)(minutes / 60) * 60;
        }
        if (minutes >= 1)
        {
            returnString += (int)minutes + "分钟";
            minutes -= (int)minutes;
        }

        if (minutes > 0)
        {
            returnString += (int)(minutes * 60) + "秒";
        }

        return returnString;
    }

    public string createReport(List<ChatProcessedData> chatProcessedDataList)
    {
        string durationComment;
        string intervalComment;
        string responseComment;
        string totalChatTimeComment;

        int flag1 = 0;
        int flag2 = 0;
        int flag3 = 0;
        int flag4 = 0;
        
        string totalComment = "";
        
        double averageDuration = 0;
        double averageInterval = 0;
        double yourResponseTime = 0;
        double myResponseTime = 0;
        DateTime firstChatTime = DateTime.Now;
        DateTime lastChatTime = DateTime.Now;
        double conversationCount = 0;
        double totalChatTime = 0;
        int myLessThanOneMinute = 0;
        int myLessThanFiveMinute = 0;
        int myLessThanTenMinute = 0;
        int myMoreThanTenMinute = 0;
        int yourLessThanOneMinute = 0;
        int yourLessThanFiveMinute = 0;
        int yourLessThanTenMinute = 0;
        int yourMoreThanTenMinute = 0;

        foreach (ChatProcessedData chatProcessedData in chatProcessedDataList)
        {
            averageDuration += chatProcessedData.AverageDuration * chatProcessedData.ConversationCount;
            averageInterval += chatProcessedData.AverageInterval * chatProcessedData.ConversationCount;
            myResponseTime += chatProcessedData.User1AverageReplyTime * chatProcessedData.ConversationCount;
            yourResponseTime += chatProcessedData.User2AverageReplyTime * chatProcessedData.ConversationCount;
            conversationCount += chatProcessedData.ConversationCount;
            totalChatTime += chatProcessedData.TotalDuration;
            myLessThanOneMinute += chatProcessedData.User1LessThanOneMinute;
            myLessThanFiveMinute += chatProcessedData.User1LessThanFiveMinutes;
            myLessThanTenMinute += chatProcessedData.User1LessThanTenMinutes;
            myMoreThanTenMinute += chatProcessedData.User1MoreThanTenMinutes;
            yourLessThanOneMinute += chatProcessedData.User2LessThanOneMinute;
            yourLessThanFiveMinute += chatProcessedData.User2LessThanFiveMinutes;
            yourLessThanTenMinute += chatProcessedData.User2LessThanTenMinutes;
            yourMoreThanTenMinute += chatProcessedData.User2MoreThanTenMinutes;
        }
        
        averageDuration /= conversationCount;
        averageInterval /= conversationCount;
        myResponseTime /= conversationCount;
        yourResponseTime /= conversationCount;

        firstChatTime = chatProcessedDataList.Min(t => t.FirstChatTime);
        lastChatTime = chatProcessedDataList.Max(t => t.LastChatTime);

        if (averageDuration < 10)
        {
            durationComment = "这挺短的，可能你们每次聊天都是直奔主题，或者只是简单的问候。不过，这也意味着你们的对话都很高效，没有拖泥带水。";
        }
        else
        {
            durationComment = "你们的聊天从不直奔主题，也绝不是简单问候。因为你们关系亲密，有说不完的话题，总喜欢绕着弯子分享生活点滴，每次对话都充满了温馨的絮语。";
            flag1 = 1;
        }

        if (averageInterval > 24)
        {
            intervalComment = "看来你们的聊天并不是天天都有，而是偶尔才联系一次。";
        }
        else
        {
            intervalComment = "看来你们的聊天十分频繁，几乎天天都不间断地畅聊。";
            flag2 = 1;
        }

        if (myResponseTime < 5 && yourResponseTime < 5)
        {
            responseComment = "这真是太快了！这说明你们一旦开始聊天，都是立刻回复，没有让对话冷场。";
        }
        else
        {
            responseComment = "这并非是因为不够重视或拖沓，而是因为你们在交流时，会认真地斟酌每一句话，仔细考量用词与表达，所以回复的间隔会稍长一些，但每一次回应都饱含着深思熟虑与真诚用心。";
            flag3 = 1;
        }
        
        responseComment += "\n  ta对你有" + yourLessThanOneMinute + "次聊天回复时间都不到1分钟；有" + yourLessThanFiveMinute + "次聊天回复时间都不到5分钟；有"
                            + yourLessThanTenMinute + "次聊天回复时间都不到10分钟；有" + yourMoreThanTenMinute + "次聊天回复时间超过10分钟；";
        
        responseComment += "\n  你对ta有" + myLessThanOneMinute + "次聊天回复时间都不到1分钟；有" + myLessThanFiveMinute + "次聊天回复时间都不到5分钟；有"
                           + myLessThanTenMinute + "次聊天回复时间都不到10分钟；有" + myMoreThanTenMinute + "次聊天回复时间超过10分钟。";

        if (totalChatTime < 300)
        {
            totalChatTimeComment = "这真的不算长，但每次聊天都很集中，没有拖拖拉拉。";
        }
        else
        {
            totalChatTimeComment = "真的蛮久的，但快乐地享受聊天总是让人感到时光短暂。";
            flag4 = 1;
        }

        if (flag2 == 0)
        {
            totalComment += "\n  总的来说，你们的聊天次数不多，";
        }
        else
        {
            totalComment += "\n  总的来说，你们的聊天次数不少，";
        }

        if (flag1 == 0)
        {
            totalComment += "每次都是快速、高效，";
        }
        else
        {
            totalComment += "每次都有说不完的话,";
        }

        if (flag3 == 0)
        {
            totalComment += "并且回复得很及时。";
        }
        else
        {
            totalComment += "并且很认真地斟酌回复。";
        }

        if (flag4 == 0)
        {
            totalComment += "可能你们都比较忙，但一旦聊起来，都是全神贯注的。";
        }
        else
        {
            totalComment += "看来你们都对与对方聊天乐此不疲，一旦聊起来，便无法自拔。";
        }

        totalComment += "\n  根据以上的情况，让我们看看怎么与ta更好地聊天吧！";
        
        if (flag2 == 0)
        {
            totalComment += "\n  你们的交流并不频繁，可以尝试主动发起聊天，增加聊天的频率哦！比如每周固定安排一个时间段进行简短交流，分享一下近期生活、工作中的趣事或者遇到的小问题等，以此加深对彼此的了解，增进感情。";
        }
        else
        {
            totalComment += "\n  你们都很喜欢和对方聊天！继续保持这样频繁交流，可以加深对彼此的了解，增进感情哦。";
        }

        if (flag1 == 0)
        {
            totalComment +=
                "\n  虽然高效的聊天很不错，但有时候可以试着在直奔主题之余，再多拓展一些相关话题，让聊天更丰富一些。例如在解决完当前的问题或者回应完问候后，接着聊聊与之相关的其他想法、感受等，这样有助于更深入地交流，使聊天更有深度和质量。";
        }
        else
        {
            totalComment +=
                "\n  你们每次都能聊这么久，真让人羡慕！继续继续，更深入地交流，使聊天更有深度和质量，让感情长长久久！";
        }

        if (flag3 == 0)
        {
            totalComment += 
                "\n  你们的回复都好及时啊！在后续聊天中请继续保持这样及时回复的好习惯，让对话能够顺畅地进行下去，避免出现长时间等待回复而导致话题中断的情况。而且可以基于对方回复的内容进一步追问、探讨，充分发挥快速响应营造出的良好交流氛围，提升聊天的连贯性和趣味性。";
        }
        else
        {
            totalComment +=
                "\n  回复认真斟酌是件好事，但及时回复能避免出现长时间等待回复而导致话题中断的情况，而且可以基于对方回复的内容进一步追问、探讨，充分发挥快速响应营造出的良好交流氛围，提升聊天的连贯性和趣味性哦。";
        }
        
        string report = "  嘿，来聊聊和" + chatProcessedDataList[0].Username2 + "的聊天情况吧！" +
                        "\n  首先，你们的聊天平均每次持续大约"+ ChangeTimeFormat(averageDuration) + "。" +
                        durationComment +
                        
                        "\n  然后，说到聊天的间隔，哇，这个数字有点惊人，平均" + ChangeTimeFormat(averageInterval*60) + "。" +
                        intervalComment +
                        
                        "\n  再来看看回复速度，ta对你平均只有"+ ChangeTimeFormat(yourResponseTime) +"的回复时间，你对ta平均只有" + ChangeTimeFormat(myResponseTime) + "的回复时间。" +
                        responseComment +
                        
                        "\n  你们的聊天跨度从"+ firstChatTime.Year +"年"+firstChatTime.Month+"月"+firstChatTime.Day+"日开始，" +
                        "一直到"+ lastChatTime.Year +"年"+lastChatTime.Month+"月"+lastChatTime.Day+"日" +
                        "，总共有"+ conversationCount +"次，每次聊天都很有意义！" +
                        
                        "\n  最后，你们的总聊天时长加起来有"+ ChangeTimeFormat(totalChatTime) + "左右，" +
                        totalChatTimeComment +
                        totalComment;
        
        return report;
    }
}